package com.github.browep.bat.activities;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.github.browep.bat.MyApplication;
import com.github.browep.bat.R;
import com.github.browep.bat.ScreenService;
import com.github.browep.bat.dao.Dao;
import org.w3c.dom.Element;
import static com.github.browep.bat.util.XmlUtil.*;
import static com.github.browep.bat.Constants.*;

/**
 * Created by IntelliJ IDEA.
 * User: paul
 * Date: 8/26/11
 * Time: 2:21 PM
 * To change this template use File | Settings | File Templates.
 */
public abstract class BaseActivity extends Activity {

    protected Activity self = this;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    protected View setupWrappedUI(int layoutId) {
        setContentView(R.layout.banner_wrapper);
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout wrapper = (LinearLayout) findViewById(R.id.wrapper_container);
        return inflater.inflate(layoutId, wrapper, true);

    }

    public MyApplication getMyApplication() {
        return (MyApplication) getApplication();
    }

    public ScreenService getScreenService() {
        return getMyApplication().getScreenService();
    }

    public Dao getDao(){
        return getMyApplication().getDao();
    }


    public View.OnClickListener onClickListenerFromElem(Element onClickElement){

        if(!"on-click".equals(onClickElement.getTagName())){
            throw new IllegalArgumentException("expected on-click element, got " + onClickElement.getTagName());
        }

        if("new-activity".equals(onClickElement.getAttribute("type"))){
            // get from the screen service the screen.
            final String id = getElementByTagName(onClickElement, "id").getTextContent();
            final ScreenService.Screen screen = getScreenService().getScreenById(id);
            return new View.OnClickListener() {
                public void onClick(View view) {
                    Intent intent = new Intent(self,screen.getActivityClass());
                    Bundle bundle = new Bundle();
                    bundle.putString(SCREEN_ID,id);
                    intent.putExtras(bundle);
                    self.startActivity(intent);
                }
            };

        }

        return null;

    }

    public void makeToast(String message){
        Toast.makeText(this,message,5000).show();
    }
}
